{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "pycharm": {}
      },
      "source": [
        "### 获取数据进行处理"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "pycharm": {}
      },
      "outputs": [],
      "source": [
        "import os\n",
        "import pdb\n",
        "import pandas as pd\n",
        "import datetime\n",
        "from jqdatasdk import *"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "pycharm": {}
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "/home/kerry/work/workenv/alpha_mind/lib/python3.6/site-packages/numpy/lib/arraysetops.py:569: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison\n",
            "  mask |\u003d (ar1 \u003d\u003d a)\n"
          ]
        },
        {
          "ename": "NameError",
          "evalue": "name \u0027vip_stock_sets\u0027 is not defined",
          "output_type": "error",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
            "\u001b[0;32m\u003cipython-input-6-282aeaea1455\u003e\u001b[0m in \u001b[0;36m\u003cmodule\u003e\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mip_stock_sets\u001b[0m \u001b[0;34m\u003d\u001b[0m \u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread_csv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u0027/kywk/data/xq/vip_stock/vip_stock_sets.csv\u0027\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mindex_col\u001b[0m\u001b[0;34m\u003d\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreset_index\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----\u003e 2\u001b[0;31m vip_stock_sets[\u0027created\u0027] \u003d vip_stock_sets[\u0027created\u0027].apply(lambda x: datetime.datetime.strptime(x, \n\u001b[0m\u001b[1;32m      3\u001b[0m                                                                                                  \u0027%Y-%m-%d %H:%M:%S\u0027))\n",
            "\u001b[0;31mNameError\u001b[0m: name \u0027vip_stock_sets\u0027 is not defined"
          ]
        }
      ],
      "source": [
        "ip_stock_sets \u003d pd.read_csv(\u0027/kywk/data/xq/vip_stock/vip_stock_sets.csv\u0027, index_col\u003d0).reset_index()\n",
        "vip_stock_sets[\u0027created\u0027] \u003d vip_stock_sets[\u0027created\u0027].apply(lambda x: datetime.datetime.strptime(x, \n",
        "                                                                                                 \u0027%Y-%m-%d %H:%M:%S\u0027))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "pycharm": {}
      },
      "outputs": [],
      "source": [
        "vip_stock_sets"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "pycharm": {}
      },
      "source": [
        "## 只保留沪深股票"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "pycharm": {}
      },
      "outputs": [],
      "source": [
        "def f(row):\n",
        "    if row[:2] \u003d\u003d \u0027SH\u0027:\n",
        "        return str(row[2:]) + \u0027.\u0027 + \u0027XSHG\u0027\n",
        "    elif row[:2] \u003d\u003d \u0027SZ\u0027:\n",
        "        return str(row[2:]) + \u0027.\u0027 + \u0027XSHE\u0027"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "scrolled": false,
        "pycharm": {}
      },
      "outputs": [],
      "source": [
        "ex_vip_stock_sets \u003d vip_stock_sets.set_index(\u0027exchange\u0027)\n",
        "xsh_market_stock \u003d ex_vip_stock_sets.loc[\u0027SZ\u0027].append(ex_vip_stock_sets.loc[\u0027SH\u0027]).reset_index()\n",
        "xsh_market_stock[\u0027symbol\u0027] \u003d xsh_market_stock[\u0027symbol\u0027].apply(f)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "pycharm": {}
      },
      "source": [
        "## 统计近段时间股票关注比例"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 22,
      "metadata": {
        "scrolled": false,
        "pycharm": {}
      },
      "outputs": [],
      "source": [
        "bwteen_time \u003d datetime.datetime(2019,2,10)\n",
        "time_marekt_stock \u003d xsh_market_stock[xsh_market_stock[\u0027created\u0027] \u003e bwteen_time]\n",
        "time_marekt_stock \u003d time_marekt_stock[[\u0027exchange\u0027,\u0027name\u0027,\u0027symbol\u0027,\u0027vid\u0027]]\n",
        "symbol_market_stock \u003d time_marekt_stock.groupby(\u0027symbol\u0027).count()\n",
        "symbol_market_stock[\u0027ratio\u0027] \u003d symbol_market_stock[\u0027vid\u0027] / len(symbol_market_stock)\n",
        "symbol_market_stock \u003d symbol_market_stock.sort_values(by\u003d\u0027ratio\u0027, ascending\u003dFalse)[[\u0027ratio\u0027,\u0027vid\u0027]]\n",
        "symbol_market_stock.rename(columns\u003d{\u0027vid\u0027:\u0027count\u0027}, inplace\u003dTrue)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 23,
      "metadata": {
        "pycharm": {}
      },
      "outputs": [],
      "source": [
        "market_stock \u003d time_marekt_stock.drop_duplicates(subset\u003d\u0027symbol\u0027, keep\u003d\u0027first\u0027, inplace\u003dFalse).set_index(\u0027symbol\u0027)\n",
        "market_stock \u003d market_stock[[\u0027name\u0027]]\n",
        "v_market_stock \u003d symbol_market_stock.merge(market_stock, left_index\u003dTrue, right_index\u003dTrue)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "pycharm": {}
      },
      "source": [
        "# 获取对应行业"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 24,
      "metadata": {
        "pycharm": {}
      },
      "outputs": [],
      "source": "industry_set \u003d [\u0027801010\u0027, \u0027801020\u0027, \u0027801030\u0027, \u0027801040\u0027, \u0027801050\u0027, \u0027801080\u0027, \u0027801110\u0027, \u0027801120\u0027, \u0027801130\u0027, \n                  \u0027801140\u0027, \u0027801150\u0027, \u0027801160\u0027, \u0027801170\u0027, \u0027801180\u0027, \u0027801200\u0027, \u0027801210\u0027, \u0027801230\u0027, \u0027801710\u0027,\n                  \u0027801720\u0027, \u0027801730\u0027, \u0027801740\u0027, \u0027801750\u0027, \u0027801760\u0027, \u0027801770\u0027, \u0027801780\u0027, \u0027801790\u0027, \u0027801880\u0027,\u0027801890\u0027]\nindustry_df \u003d pd.DataFrame(columns\u003dv_market_stock.index)\nfor industry in industry_set:\n    industry_stocks \u003d get_industry_stocks(industry)\n    industry_stocks \u003d list(set(industry_stocks)\u0026set(v_market_stock.index))\n    industry_df.loc[\u0027industry\u0027,industry_stocks] \u003d industry\n\nindustry_df \u003d industry_df.T.dropna()\nindustry_df.reset_index(inplace \u003d True)\nindustry_df.set_index(\u0027symbol\u0027,inplace\u003dTrue)"
    },
    {
      "cell_type": "code",
      "execution_count": 25,
      "metadata": {
        "pycharm": {}
      },
      "outputs": [],
      "source": [
        "industry_market_stock \u003d v_market_stock.merge(industry_df,left_index\u003dTrue, right_index\u003dTrue)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 26,
      "metadata": {
        "pycharm": {}
      },
      "outputs": [],
      "source": [
        "industry_set \u003d [\n",
        "    {\u0027industry\u0027:\u0027801010\u0027,\u0027indname\u0027:\u0027农林牧渔I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801020\u0027,\u0027indname\u0027:\u0027采掘I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801030\u0027,\u0027indname\u0027:\u0027化工I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801040\u0027,\u0027indname\u0027:\u0027钢铁I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801050\u0027,\u0027indname\u0027:\u0027有色金属I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801080\u0027,\u0027indname\u0027:\u0027电子I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801110\u0027,\u0027indname\u0027:\u0027家用电器I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801120\u0027,\u0027indname\u0027:\u0027食品饮料I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801130\u0027,\u0027indname\u0027:\u0027纺织服装I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801140\u0027,\u0027indname\u0027:\u0027轻工制造I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801150\u0027,\u0027indname\u0027:\u0027医药生物I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801160\u0027,\u0027indname\u0027:\u0027公用事业I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801170\u0027,\u0027indname\u0027:\u0027交通运输I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801180\u0027,\u0027indname\u0027:\u0027房地产I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801200\u0027,\u0027indname\u0027:\u0027商业贸易I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801210\u0027,\u0027indname\u0027:\u0027休闲服务I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801230\u0027,\u0027indname\u0027:\u0027综合I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801710\u0027,\u0027indname\u0027:\u0027建筑材料I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801720\u0027,\u0027indname\u0027:\u0027建筑装饰I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801730\u0027,\u0027indname\u0027:\u0027电气设备I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801740\u0027,\u0027indname\u0027:\u0027国防军工I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801750\u0027,\u0027indname\u0027:\u0027计算机I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801760\u0027,\u0027indname\u0027:\u0027传媒I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801770\u0027,\u0027indname\u0027:\u0027通信I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801780\u0027,\u0027indname\u0027:\u0027银行I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801790\u0027,\u0027indname\u0027:\u0027非银金融I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801880\u0027,\u0027indname\u0027:\u0027汽车I\u0027},\n",
        "    {\u0027industry\u0027:\u0027801890\u0027,\u0027indname\u0027:\u0027机械设备I\u0027}]\n",
        "market_industry \u003d pd.DataFrame(industry_set).set_index(\u0027industry\u0027)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 27,
      "metadata": {
        "pycharm": {}
      },
      "outputs": [],
      "source": [
        "industry_market_stock \u003dindustry_market_stock.reset_index().set_index(\n",
        "    \u0027industry\u0027).merge(market_industry,left_index\u003dTrue, right_index\u003dTrue)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 28,
      "metadata": {
        "scrolled": false,
        "pycharm": {}
      },
      "outputs": [],
      "source": [
        "result_socket \u003d industry_market_stock.sort_values(by\u003d\u0027count\u0027, ascending\u003dFalse)[:50]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 29,
      "metadata": {
        "pycharm": {}
      },
      "outputs": [],
      "source": [
        "result_socket.to_csv(bwteen_time.strftime(\"%Y-%m-%d\") + \u0027_stock_50.csv\u0027,encoding\u003d\u0027UTF-8\u0027)\n",
        "result_socket.groupby(\u0027indname\u0027).count().to_csv(bwteen_time.strftime(\"%Y-%m-%d\") + \u0027group_50.csv\u0027,encoding\u003d\u0027UTF-8\u0027)\n",
        "\n",
        "industry_market_stock.groupby(\u0027indname\u0027).count().to_csv(bwteen_time.strftime(\"%Y-%m-%d\") + \u0027group_all.csv\u0027,encoding\u003d\u0027UTF-8\u0027)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "pycharm": {}
      },
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}